<?php

/**
 * 
 * @author
 * @version 
 */

class Merchant_StocktakeController extends Zend_Controller_Action {
	
	public function preDispatch(){
		$this->tplDirectory = "merchant/stocktake/";
	}
	
	public function indexAction() {
		
		echo Cff::renderTpl($this->tplDirectory."index.tpl","user-layout");
	}

	public function listAction() {		

		$customerService = new Merchant_Service_Customer();
		$customerInfo = $customerService->getLoginInfo()->customer;
		$customer_id = $customerInfo['customer_id'];
		
		$cond = $rowArr = array();
		$cond['customer_id'] = $customer_id = 'A0359';	
		$orderby = array('tsi_id desc');			
		$total = Merchant_Service_TakeStockItem::getByCondGroup($cond,'count(*)',0,0,array());
		$rows = Merchant_Service_TakeStockItem::getByCondGroup($cond,'*',0,0,$orderby);
		$tsStatus = Merchant_Service_TakeStock::$tsStatus;		
		foreach ($rows as $key => $val){
			$row = Merchant_Service_TakeStock::getByfield('ts_code',$val['ts_code']);
			$row['ts_status'] = $tsStatus[$row['ts_status']];
			$rowArr[] = $row;
		}
		
		$data =array('ask'=>0);		
		if($total){
			$data['ask'] = 1;
			$data['result'] = $rowArr;
		}
		
		die(json_encode($data));
	}
	
	public function detailAction() {
		$ts_code = $this->_request->getParam('code','');
		$this->view->ts_code = $ts_code;

		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$ts_code = $this->_request->getParam('code','');
			
			$cond = $rowArr = array();
			$cond['customer_id'] = $customer_id = 'A0359';
			$ts_code && $cond['ts_code'] = $ts_code;
			$orderby = array('tsi_id desc');
			$total = Merchant_Service_TakeStockItem::getByCond($cond,'count(*)',0,0,array());
			$rows = Merchant_Service_TakeStockItem::getByCond($cond,'*',$pageSize,$page,$orderby);
			foreach ($rows as $key => $val){
				$productInfo = Merchant_Service_Product::getByProductId($val['product_id']);
				$rows[$key]['product_sku'] = $productInfo['product_sku'];
			}
			
			$data =array('ask'=>0,'total'=>0);	
			if($total){
				$data['ask'] = 1;
				$data['total'] = $total;
				$data['result'] = $rows;
			}
			die(json_encode($data));
		}
	
		echo Cff::renderTpl($this->tplDirectory.'detail.tpl','layout-blank');
	}
	
	
}
